#include "EquipSelectList.h"
#include "EquipItem.h"
#include "../../model/proxy/ItemProxy.h"
#include "EquipItemLoader.h"

EquipSelectList::EquipSelectList(void)
	:mSelectMode(MultiSelect)
{
}

EquipSelectList::~EquipSelectList(void)
{
}

bool EquipSelectList::init()
{
	mScrollView = CCScrollView::create(CCSizeMake(320, 300));
	mScrollView->setDirection(CCScrollViewDirectionVertical);
	mScrollView->setPosition(ccp(0, 115));
	addChild(mScrollView);

	return true;
}

void EquipSelectList::onEnter()
{
	CCNode::onEnter();
}

void EquipSelectList::onExit()
{
	CCNode::onExit();
}

void EquipSelectList::refresh()
{
	RegisterLoader("EquipItem", EquipItemLoader::loader());
	EquipList equipList;
	ItemProxy::getSingletonPtr()->getPutOnList(equipList);
	CCPoint contentPos = ccp(0, equipList.size() * 74);
	CCLayer* content = CCLayer::create();
	content->setContentSize(CCSizeMake(320, contentPos.y));
	mScrollView->setContainer(content);
	mScrollView->setContentOffset(ccp(0, mScrollView->getViewSize().height-contentPos.y));
	for(int i = 0; i < equipList.size(); i++)
	{
		EquipItem* item = (EquipItem*)ReadNode("EquipItem.ccbi", this);
		item->setIsClickBg(false);
		item->showCheckBox(true);
		item->mCheckBox->setTarget(this, callfuncO_selector(EquipSelectList::onCheckBoxClick));
		item->setData(equipList[i]);
		item->refresh();
		item->setPosition(ccp(0, contentPos.y - (i+1) * 74));
		content->addChild(item);
		mCurList.push_back(item);
	}
}

vector<int>& EquipSelectList::getSelectList()
{
	mSelectList.clear();
	vector<ListItem*>::iterator iter = mCurList.begin();
	while(iter != mCurList.end())
	{
		EquipItem* equipItem = (EquipItem*)(*iter);
		if(equipItem->isSelect())
		{
			EquipInfo* equipInfo = (EquipInfo*)equipItem->getData();
			mSelectList.push_back(equipInfo->index);
		}
		iter++;
	}
	return mSelectList;
}

void EquipSelectList::getSelectList( vector<int>& seletList )
{
	seletList.clear();
	vector<ListItem*>::iterator iter = mCurList.begin();
	while(iter != mCurList.end())
	{
		EquipItem* equipItem = (EquipItem*)(*iter);
		if(equipItem->isSelect())
		{
			EquipInfo* equipInfo = (EquipInfo*)equipItem->getData();
			seletList.push_back(equipInfo->index);
		}
		iter++;
	}
}

void EquipSelectList::onCheckBoxClick( CCObject* object )
{
	CheckBox* checkbox = (CheckBox*)object;
	if (mSelectMode == SingleSelect)
	{
		if (checkbox->getIsSelect())
		{
			vector<ListItem*>::iterator iter = mCurList.begin();
			while(iter != mCurList.end())
			{
				EquipItem* equipItem = (EquipItem*)(*iter);
				if (equipItem->mCheckBox != checkbox)
				{
					equipItem->mCheckBox->unselect();
				}
				iter++;
			}
		}
	}
}
